package com.snda.lib.util;

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URLEncoder;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;

/**
 * 
 * @author zhangzhongqing
 * 
 *         大杂烩类，不能放到其余的Util的时候就放入到该类里
 */
public class MiscHelper {

	/**
	 * Generate the multi-part post body providing the parameters and boundary
	 * string
	 * 
	 * @param parameters
	 *            the parameters need to be posted
	 * @param boundary
	 *            the random string as boundary
	 * @return a string of the post body
	 */
	public static String encodePostBody(Bundle parameters, String boundary) {
		if (parameters == null)
			return "";
		StringBuilder sb = new StringBuilder();

		for (String key : parameters.keySet()) {
			if (parameters.getByteArray(key) != null) {
				continue;
			}

			sb.append("Content-Disposition: form-data; name=\"" + key
					+ "\"\r\n\r\n" + parameters.getString(key));
			sb.append("\r\n" + "--" + boundary + "\r\n");
		}

		return sb.toString();
	}

	public static String encodeUrl(Bundle parameters) {
		if (parameters == null) {
			return "";
		}

		StringBuilder sb = new StringBuilder();
		boolean first = true;
		for (String key : parameters.keySet()) {
			if (first)
				first = false;
			else
				sb.append("&");
			sb.append(URLEncoder.encode(key) + "="
					+ URLEncoder.encode(parameters.getString(key)));
		}
		return sb.toString();
	}

	/**
	 * 是否是空字符串
	 * 
	 * @param s
	 * @return
	 */
	public static boolean isEmpty(String s) {
		return s == null || s.equals("");
	}

	/**
	 * 给定的字符串是否是数字
	 * 
	 * @param str
	 * @return
	 */
	public static boolean isNumber(String str) {
		for (int i = 0; i < str.length(); i++) {
			char ch = (char) str.charAt(i);
			if (Character.isDigit(ch) == false)
				return false;
		}
		return true;
	}

	/**
	 * 判断给定的字符串是否是邮箱
	 * 
	 * @param strEmail
	 * @return
	 */
	public static boolean isEmail(String strEmail) {
		String reg = "^([a-z0-9A-Z]+[-|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$";
		boolean isValidEmail = strEmail.matches(reg);
		return isValidEmail;
	}

	/**
	 * 将dp转换成px
	 * 
	 * @param context
	 * @param dipValue
	 * @return
	 */
	public static int dp2px(Context context, float dipValue) {
		final float scale = context.getResources().getDisplayMetrics().density;
		return (int) (dipValue * scale + 0.5f);
	}

	/**
	 * 将px转换成dp
	 * 
	 * @param context
	 * @param pxValue
	 * @return
	 */
	public static int px2dp(Context context, float pxValue) {
		final float scale = context.getResources().getDisplayMetrics().density;
		return (int) (pxValue / scale + 0.5f);
	}

	/**
	 * 在Pref中写入内容
	 * 
	 * @param context
	 * @param name
	 * @param val
	 * @return
	 */
	public static boolean setPrefer(Context context, String strPrefName,
			String strKeyName, String strKeyValue) {
		SharedPreferences settings = context.getSharedPreferences(strPrefName,
				Context.MODE_PRIVATE);
		SharedPreferences.Editor editor = settings.edit();
		if (strKeyValue == null) {
			editor.remove(strKeyName);
		} else {
			editor.putString(strKeyName, strKeyValue);
		}
		return editor.commit();
	}

	/**
	 * 从Pref中读取内容
	 * 
	 * @param context
	 * @param name
	 * @return
	 */
	public static String getPrefer(Context context, String strPrefName,
			String strKeyName) {
		SharedPreferences settings = context.getSharedPreferences(strPrefName,
				Context.MODE_PRIVATE);
		return settings.getString(strKeyName, "");
	}
	
	/**
	 * 将字符串转换为流
	 * 
	 * @param str
	 * @return
	 */
	public static InputStream String2InputStream(String str) {
		ByteArrayInputStream stream = new ByteArrayInputStream(str.getBytes());
		return stream;
	}

	/**
	 * 将流转换为字符串
	 * 
	 * @param is
	 * @return
	 */
	public static String inputStream2String(InputStream is) {
		BufferedReader in = new BufferedReader(new InputStreamReader(is));
		StringBuffer buffer = new StringBuffer();
		String line = "";
		try {
			while ((line = in.readLine()) != null) {
				buffer.append(line);
			}
		} catch (IOException e) {
			return null;
		}

		return buffer.toString();
	}
}